#随机森林
#导入所需要的包
from sklearn.metrics import precision_score
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report#评估报告
from sklearn.model_selection import cross_val_score #交叉验证
from sklearn.model_selection import GridSearchCV #网格搜索
import matplotlib.pyplot as plt#可视化
import seaborn as sns#绘图包
from sklearn.preprocessing import StandardScaler,MinMaxScaler,MaxAbsScaler#归一化，标准化
# 忽略警告
import warnings
warnings.filterwarnings("ignore")
datas=pd.read_csv('data_20221030(1).csv')
datas=datas[datas.columns[2:]]
 
model=RandomForestClassifier(max_depth=8,n_estimators=76,min_samples_leaf=5,min_samples_split=7,max_features='sqrt',criterion='entropy')
# 训练模型
model.fit(X_train,y_train)
# 预测值
y_pred = model.predict(X_test)
 
'''
评估指标
'''
# 求出预测和真实一样的数目
true = np.sum(y_pred == y_test )
print('预测对的结果数目为：', true)
print('预测错的的结果数目为：', y_test.shape[0]-true)
# 评估指标
from sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score,cohen_kappa_score
print('预测数据的准确率为： {:.4}%'.format(accuracy_score(y_test,y_pred)*100))
print('预测数据的精确率为：{:.4}%'.format(
      precision_score(y_test,y_pred)*100))
print('预测数据的召回率为：{:.4}%'.format(
      recall_score(y_test,y_pred)*100))
# print("训练数据的F1值为：", f1score_train)
print('预测数据的F1值为：',
      f1_score(y_test,y_pred))
print('预测数据的Cohen’s Kappa系数为：',
      cohen_kappa_score(y_test,y_pred))
# 打印分类报告
print('预测数据的分类报告为：','\n',
      classification_report(y_test,y_pred))